package com.wyp168.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName SaleTicket
 * @Description
 * @Author wyp168
 * @Date 2022/6/28 08:03
 */
public class SaleTicket {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sales();}
            },"A").start();
        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sales();}
        },"B").start();
        new Thread(() -> {
            for (int i = 0; i < 60; i++) {
                ticket.sales();}
        },"C").start();
    }

}


class Ticket {
    private int ticketNum = 50;
    Lock lock = new ReentrantLock();
    public void sales () {
        lock.lock();;

        try {
            if (ticketNum > 0) {
                System.out.println(Thread.currentThread().getName() + "  卖出了第" + ticketNum-- + "张票，当前剩余" + ticketNum + "张票");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
